<template>
  <div>
    <!--卡片/查询输入框和查询按钮/添加按钮-->
    <div>
      <el-card id="elcard">
        <el-button
          class="addbtn"
          type="primary"
          @click="dialogFormVisible = true"
          >添加汽车品牌</el-button
        >
      </el-card>
    </div>
    <!--添加的表单-->
    <div>
      <el-dialog title="添加汽车品牌" :visible.sync="dialogFormVisible">
        <!--                  <el-select v-model="addform.brand" placeholder="请选择汽车品牌">-->
        <!--                      <el-option label="区域一" value="shanghai"></el-option>-->
        <!--                      <el-option label="区域二" value="beijing"></el-option>-->
        <!--                  </el-select>-->
        <el-form :model="addform">
          <el-form-item label="品牌名称">
            <el-input v-model="addform.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCar">添 加</el-button>
        </div>
      </el-dialog>
    </div>
    <!--列表-->
    <div>
      <el-table :data="carbrand.list" border style="width: 100%" :header-cell-style="headClass" :cell-style="rowClass">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column prop="carbrandId" label="编号">
        </el-table-column>
        <el-table-column prop="carbrandName" label="汽车品牌">
        </el-table-column>
        <el-table-column prop="action" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--修改表单-->
    <div>
      <el-dialog title="修改品牌" :visible.sync="dialogFormEditVisible">
        <el-form :model="editform">
          <el-form-item label="品牌编号 :">
            <el-input v-model="editform.id" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="品牌名称 :">
            <el-input v-model="editform.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormEditVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveDept(editform.id)"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </div>
    <!--分页-->
    <div class="fenye">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="5"
        :total="carbrand.total"
        @current-change="getcutpage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "CarBrand",
  data() {
    return {
      carbrand: [],
      dialogFormVisible: false,
      dialogFormEditVisible: false,
      input: "",
      addform: {
        brand: ""
      },
      editform: {
        name: "",
        id: ""
      }
    };
  },
  created() {
    this.getCarList(1, 5);
  },
  methods: {
    //获取数据渲染表格
    getCarList(page, limit) {
      this.$axios
        .post(
            this.$api.contentManege.carBrand.page,
          JSON.stringify({
            page: page,
            limit: limit
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          console.log(response);
          var result = response.data;
          if (response.status === 200) {
            this.carbrand = response.data.data;
            console.log(this.carbrand);
          } else {
            this.$alert(result.message);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //分页
    getcutpage: function(val) {
      console.log(val);
      this.getCarList(val, 5);
    },
    //添加表单的保存按钮
    addCar: function() {
      this.$axios
        .post(
            this.$api.contentManege.carBrand.add,
          JSON.stringify({
            carbrandName: this.addform.name
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          console.log(response);
          var result = response.data;
          if (response.status === 200) {
            this.$alert("添加成功");
            this.getCarList(1,5);
            this.dialogFormVisible = false;
          } else {
            this.$alert(result.message);
          }
        })
        .catch(err => {
          this.$alert("请求出错");
          console.log(err);
        });
    },
    //修改按钮渲染表单
    edit: function(obj) {
      console.log(obj);
      this.dialogFormEditVisible = true;
      this.editform.id = obj.carbrandId;
      this.editform.name = obj.carbrandName;
    },
    //修改表单的保存按钮
    saveDept: function() {
      // 将修改的数据发给服务器，接收服务器的响应并处理
      this.$axios
        .post(
          this.$api.contentManege.carBrand.update,
          JSON.stringify({
            carbrandName: this.editform.name,
            carbrandId: this.editform.id
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          var result = response.data;
          if (response.status === 200) {
            this.$alert("修改成功");
            this.getCarList(1,5);
            this.dialogFormEditVisible = false;
          } else {
            this.$alert(result.message);
          }
        })
        .catch(() => {
          this.$alert("请求出错");
        });
    },
    rowClass() { //表格数据居中显示
      return "text-align:center"
    },
    headClass() { //表头居中显示
      return "text-align:center"
    },
  }
};
</script>

<style scoped>
.el-input {
  margin-top: 20px !important;
}
#elcard {
  margin-bottom: 20px;
  padding: 0;
}
.fenye {
  margin-top: 20px;
}
.cell {
  text-align: center;
}
.addbtn {
  float: left;
  margin: 20px;
}
.el-card__body {
  padding: 0 !important;
}
.search_input {
  width: 300px;
  margin: 0 10px;
}
.search {
  float: left;
}
.el-dialog {
  width: 400px;
}
/deep/ .el-card__body {
  padding: 0;
}
</style>
